<?php
/**
 * File containing the ShuffleStringProcessor
 *
 * PHP Version 5
 *
 * @category Processors
 * @package  Processors
 * @author   Martin Schlecht <herrlollinger@gmail.com>
 * @license  MIT License
 * @link     http://code.google.com/p/universal-test-data
 */

/**
 * Processor class "ShuffleStringProcessor"
 *
 * Processor which is used to process tags which implements string shuffeling within the text.
 *
 * @category   Processors
 * @package    Processors
 * @subpackage ShuffleProcessors
 * @author     Martin Schlecht <herrlollinger@gmail.com>
 * @copyright  see @author
 * @license    MIT License
 * @link       http://code.google.com/p/universal-test-data
 *
 * The followings are the available attributes of the processor class
 * @property string $text, the text which should be processed
 */
class ShuffleStringProcessor
{
	private $text;
	
	/**
	 * The processor randomizes the given parameters and repeat them n times
	 *
	 * @param String $text, the text to process
	 *
	 * @return String
	 */
	public function process($text) {
		$this->text = $text;
		
		preg_match_all("#\s*{(.*?)}#si", $this->text, $matches);
		
		foreach ($matches[1] AS $m) {
			$string = "{" . $m . "}";
			$m = explode(":", $m);
			
			/**
			 * {shuffleString:(one;two;three),5}
			 * Shuffles the strings one, two and three. Probability for each value is the same
			 */
			if ($m[0] == "shuffleString") {
				$parameter = str_replace(array("(", ")"), array("", ""), $m[1]);
				$parameter = explode(",", $parameter);
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 1;
				}
				
				$values = explode(";", $parameter[0]);
				
				if (sizeof($values) <= 0) {
					return "[Misspelled input]";
				}
				
				if (Yii::app()->session[$string . "times"] >= $parameter[1] || !isset(Yii::app()->session[$string . "times"])) {
					shuffle($values);
					
					Yii::app()->session[$string . "replacement"] = $values[0];
					Yii::app()->session[$string . "times"] = 1;
									
					$replacement = $values[0];
				} else {
					$replacement = Yii::app()->session[$string . "replacement"];
					$times = Yii::app()->session[$string . "times"] + 1;
					
					Yii::app()->session[$string . "times"] = $times;
				}
				
				$this->text = str_replace($string, $replacement, $this->text);
			}
			
			/**
			 * {shuffleStringProb:(one;1)(two;2)(three;3),5}
			 * Shuffles the strings one, two and three. Probability for each value is given by the second parameter corresponding to each string
			 */
			if ($m[0] == "shuffleStringProb") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 1;
				}
				
				if (Yii::app()->session[$string . "_times"] >= $parameter[1] || !isset(Yii::app()->session[$string . "_times"])) {
					$parameter = str_replace(array(")("), array("),("), $parameter[0]);
					$parameter = str_replace(array("(", ")"), array("", ""), $parameter);
					
					$values = array();
					
					$parameter = explode(",", $parameter);
					
					if (sizeof($parameter) <= 0) {
						return "[Misspelled input]";
					}
					
					foreach ($parameter AS $p) {
						
						$value = explode(";", $p);
						
						if (!isset($value[0])) {
							return "[Misspelled input]";
						}
						
						if (!isset($value[1]) || !is_numeric($value[1])) {
							$value[1] = 1;
						}
											
						for ($x = 0; $x < $value[1]; $x++) {
							$values[] = $value[0];
						}					
					}
					
					shuffle($values);
					
					Yii::app()->session[$string . "_replacement"] = $values[0];
					Yii::app()->session[$string . "_times"] = 1;
									
					$replacement = $values[0];
				} else {
					$replacement = Yii::app()->session[$string . "_replacement"];
					$times = Yii::app()->session[$string . "_times"] + 1;
					
					Yii::app()->session[$string . "_times"] = $times;
				}
				
				Yii::app()->session['resetManager']->add($string . "_replacement");
				Yii::app()->session['resetManager']->add($string . "_times");
								
				$this->text = str_replace($string, $replacement, $this->text);
			}
		}		
		
		return $this->text;
	}
	
	/**
	 * Returns the documentation
	 *
	 * @return String
	 */
	 public function documentation() {
		return array(
			array('{shuffleString:(one;two;three),5}', 'Shuffles the given strings and repeat the selected value 5 times'), 
			array('{shuffleStringProb:(one;1)(two;2)(three;3),5}', 'Shuffles the given strings according to their given propability and repeat the selected value 5 times'), 
		);
	 }
	 
	/**
	 * Prints the documentation
	 *
	 * @return string
	 */
	public function print_documentation() {
		foreach ($this->documentation() AS $docu) {
			echo '<span style="font-family:monospace;font-size:11px">' . $docu[0] . '</span><br/>' . $docu[1] . '<br/>';
		}
	}
}
?>